/*
luxuan 2017-11-8 14:29:44
* */
loadiframe();
/*
=============================================================Tabs初始化设置=============================================================================
*/
var number = 1;
function addTab(title, url,DescendantsId){
	var DomIframe = $('#tt');
	if(DescendantsId == undefined || DescendantsId == ' ' || DescendantsId == null){
		if (DomIframe.tabs('exists', title)){
			$('#tt').tabs('select', title);
		} else {
			let content = '<iframe scrolling="auto" frameborder="0" data-name="'+title+'" data-src="'+url+'" id="iframeId'+number+'"  src="'+url+'" style="width:100%;height:100%;"></iframe>';
			DomIframe.tabs('add',{
				title:title,
				content:content,
				closable:true
			});
			var thisiframe = DomIframe.tabs('getSelected');
			var tab = thisiframe.panel('options').tab;
			thisiframe.height(loadiframe());
		}
	}else{
		if (DomIframe.tabs('exists', title)){
			$('#tt').tabs('select', title);
		} else {
			var content = '<iframe scrolling="auto" data-parentName="'+DescendantsId+'"  frameborder="0" data-name="'+title+'" data-src="'+url+'" id="iframeId'+number+'"  src="'+url+'" style="width:100%;height:100%;"></iframe>';
			DomIframe.tabs('add',{
				title:title,
				content:content,
				closable:true
			});
			var thisiframe = DomIframe.tabs('getSelected');
			var tab = thisiframe.panel('options').tab;
			thisiframe.height(loadiframe());
		}
	}

	number++
}

/*
=============================================================================计算iframe高度=========================================================
*/
function loadiframe(){
	var VH = $(window).height();
	var tophdH = $('.top-hd').outerHeight(true);
	var btmftH = $('.btm-ft').outerHeight(true);
	var iframeH = VH-(tophdH+btmftH);
	$('.main-cont1').height(iframeH);
	$('iframe').height(iframeH);
	return iframeH;
}

function sizeHasBeenChanged(){
	var stateVal= window.localStorage.getItem("state");
	if(stateVal == false || stateVal == "false"){
		$('iframe').width($(window).width()-190);
		$('.icon-mini-refresh').width($(window).width()-190);
		$('.panel-body-noheader').width($(window).width()-190);
		$('.tabs-wrap').width($(window).width()-190);
		$('.panel').width($(window).width()-190);
	}else{
		$('iframe').width($(window).width());
		$('.icon-mini-refresh').width($(window).width());
		$('.panel-body-noheader').width($(window).width());
		$('.tabs-wrap').width($(window).width());
		$('.panel').width($(window).width());
	}
}
/*
===========================================================================操作页面样式===================================================================
*/
$(function(){
	'use strict';
	//左侧导航菜单效果
	$('.side-menu li dt').click(function(){
		$(this).parents('li').addClass('open');
		$(this).parents('.side-menu').find('.InitialPage').removeClass('active');
		$(this).parents('li').siblings().removeClass('open');

	});
	$('.side-menu dd').click(function(){
		$(this).parents('li').addClass('open');
		$(this).parents('li').siblings().removeClass('open')
	});
	$('.side-menu li').each(function(){
		//判断链接相当（当前页面导航样式）
		if($(this).find('a').attr('href') == window.location.pathname){
			$(this).addClass('open');
			$(this).siblings().find('.InitialPage').removeClass('active');
			$('.InitialPage').removeClass('active');
		}else if($('.side-menu h2 a').attr('href') == window.location.pathname){
			$('.InitialPage').addClass('active');
		}
	});
	//Tab选项卡.
	$('.tab-nav li').click(function(){
		var liIndex = $('.tab-nav li').index(this);
		$(this).addClass('active').siblings().removeClass('active');
		$('.tab-cont').eq(liIndex).fadeIn().siblings('.tab-cont').hide();
	});
	//Button下拉菜单
	$('.btn-drop-group .btn').click(function(e){
		$(this).siblings('.drop-list').show();
		$(this).parent().siblings().find('.drop-list').hide();
		$(document).one('click', function(){
			$('.btn-drop-group .drop-list').hide();
		});
		e.stopPropagation();
	});
	//点击list将当前值复制于button
	$('.btn-drop-group .drop-list li').click(function(){
		$(this).parent().parent().hide();
	});
	//左侧菜单收缩
	$('.top-hd .icon-reorder').click(function(){
		loadiframe();
		$('.side-nav').toggleClass('hide');
		$('.top-hd').toggleClass('tophd');
		$('.top-hd,.main-cont,.btm-ft').toggleClass('switchMenu');
		$('.top-hd .hd-lt a').toggleClass('icon-exchange');
		localStorage.setItem('setLayOut1','hide');
		localStorage.setItem('setLayOut2','switchMenu');
		localStorage.setItem('setLayOut4','tophd');
		localStorage.setItem('setLayOut3','icon-exchange');
		if(!$('.side-nav').hasClass('hide')){
			localStorage.removeItem('setLayOut1');
			localStorage.removeItem('setLayOut2');
			localStorage.removeItem('setLayOut4');
			localStorage.removeItem('setLayOut3');
			window.localStorage.setItem('state','false');
			$('iframe').width($(window).width()-190);
			$('.icon-mini-refresh').width($(window).width()-190);
			$('.panel-body-noheader').width($(window).width()-190);
			$('.tabs-wrap').width($(window).width()-190);
			$('.panel').width($(window).width()-190);
		}else{
			window.localStorage.setItem('state','true');
			$('iframe').width($(window).width());
			$('.icon-mini-refresh').width($(window).width());
			$('.panel-body-noheader').width($(window).width());
			$('.tabs-wrap').width($(window).width());
			$('.panel').width($(window).width());
		}
	});
	var stateVal= window.localStorage.getItem("state");
	if(stateVal == 'false' || stateVal == false || stateVal == undefined || stateVal == 'undefined'){
		loadiframe()
	}else{
		loadiframe();
		$('iframe').parent().width(100+'%')
	}
	$('.side-nav').addClass(localStorage.getItem('setLayOut1'));
	$('.top-hd,.main-cont,.btm-ft').addClass(localStorage.getItem('setLayOut2'));
	$('header.top-hd').addClass(localStorage.getItem('setLayOut4'));
	$('.top-hd .hd-lt a').addClass(localStorage.getItem('setLayOut3'));


	//弹出层基础效果（确认/取消/关闭）
	$('.JyesBtn').click(function(){
		$(this).parents('.dialog').hide();
		if($('.mask').attr('display','block')){
			$('.mask').hide();
		}
	});
	$('.JnoBtn,.JclosePanel').click(function(){
		$(this).parents('.dialog').hide();
		if($('.mask').attr('display','block')){
			$('.mask').hide();
		}
	});
	//基础弹出窗
	$('.JopenPanel').click(function(){
		$('.dialog').show();
		$('.dialog').css('box-shadow','0 0 30px #d2d2d2');
	});
	//带遮罩
	$('.JopenMaskPanel').click(function(){
		$('.dialog,.mask').show();
		$('.dialog').css('box-shadow','none');
	});
	$('.mask').click(function(){
		$(this).hide();
		$('.dialog').hide();
	});
	//自动关闭消息窗口
	$('.Jmsg').click(function(){
		$('dialog').show().delay(5000).hide(0);
	});
	//安全退出
	$('#JsSignOut').click(function(){
		layer.confirm('确定登出管理中心？', {
			title:'系统提示',
			btn: ['确定','取消']
		}, function(){
			location.href = 'login.html';
		});
	});
	//显示隐藏操作版
	$('.userJurisdiction').on('click',function(){
		$(".popup_user").toggleClass('open');
		$('.mask').toggleClass('open');
	});
	$(document).bind("click",function(e){
		var target = $(e.target);
		if(target.closest(".userJurisdiction").length == 0){
			$(".popup_user").removeClass('open');
			$('.mask').removeClass('open');
		}
	});
	$('.mask').bind("click",function(e){
		$(this).toggleClass('open');
		$(".popup_user").toggleClass('open');
	})
});

/*
==============================================================================获取当前iframe对象==============================================================*/

function iframeObj(){
	var dataName;
	var iframeId= self.frameElement.getAttribute('id');//获取id
	var bodyrel="temp"+getRandom(100);
	var tIndex=0;
	$('body').attr("rel",bodyrel);
	$(window.parent.document).find("iframe").each(function(){
		var tRel=$(this.contentWindow.document).find("body").attr("rel");
		if(tRel==bodyrel){
			dataName = $(this);
		}
	});
	return dataName;
}
function getRandom(n){
	return Math.floor(Math.random()*n+1)
}


/*
=========================================================================刷新当前Iframe==================================================================
*/
function reloadThisIframe(){
	let data = iframeObj();
	window.parent.getFindIframe(data.attr('data-name'),'reload');
}




/*
=======================================================================关闭当前Iframe=================================================================
*/
function CloseThisIframe(){
	let data = iframeObj();
	window.parent.getFindIframe(data.attr('data-name'),'Close');
}




/*
 ==================================================================在当前Iframe下重新打开一个子页面=====================================================
 */
function openNewIframe(){
	let data = iframeObj();
	window.parent.addTab('新增客户信息','admin/customer/add.html',data.attr('data-name'));
}




/*
 ======================================================================关闭当前页刷新父页面============================================================
 */
function CloseThisReloadParent(){
	let data = iframeObj();
	window.parent.getFindIframe(data.attr('data-name'),'CloseReload',data.attr('data-parentName'));
}

/*
 =========================================================================修改密码==================================================================
 */
function ModifyPassword(){
	const viveW = $(window).width()/2,
		  viveH = $(window).height()-60;
	layer.open({
		type: 1,
		skin: 'layui-layer-demo',
		title:false,
		closeBtn: 2,
		area: ['562px', '412px'],
		shade: [0.03, '#000'],
		shadeClose: true, //开启遮罩关闭
		scrollbar: false,//屏蔽浏览器出现滚动条
		content: $('#ModifyPassword')
	});
}
/*
 =========================================================================验证提交修改密码================================================================
 */
function confirmBtn(){
	const 	passwordID = $('#passwordID'),
		VerificationPassword = $('#VerificationPassword'),
		passwordVal = $.trim(passwordID.val()),
		VerificationPasswordVal = $.trim(VerificationPassword.val()),
		errorMsBox = $('#errorMs-box'),
		errorMs = $('#errorMs');
	if(passwordVal == ''|| passwordVal == null || passwordVal == undefined){
		errorMsBox.css('visibility','inherit');
		errorMs.text('请输入新密码');
		passwordID.focus();
		return false;
	}else{
		errorMsBox.css('visibility','hidden');
		errorMs.text(' ');
		VerificationPassword.focus();
	}
	if(passwordVal.length < 6 || passwordVal.length > 18){
		errorMsBox.css('visibility','inherit');
		errorMs.text('鉴于安全考虑,新密码长度请保持在 6 ~ 18 位');
		passwordID.focus();
		return false;
	}
	if(VerificationPasswordVal == ''|| VerificationPasswordVal == null || VerificationPasswordVal == undefined){
		errorMsBox.css('visibility','inherit');
		errorMs.text('请确认新密码');
		VerificationPassword.focus();
		return false;
	}else{
		errorMsBox.css('visibility','hidden');
		errorMs.text(' ');
	}
	if(passwordVal != VerificationPasswordVal){
		errorMsBox.css('visibility','inherit');
		errorMs.text('两次输入密码不一致,请重新输入.');
		VerificationPassword.focus();
		return false
	}else{
		errorMsBox.css('visibility','hidden');
		errorMs.text(' ');
	}
	layer.msg('加载中...', {
		icon: 16,
		shade: 0.01,
		time:false
	});
}